﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblBusinessNatureDAL:ImtblBusinessNature
    {
        HKEA_CMSEntities model;
        mtblBusinessNature nature;
        public mtblBusinessNatureDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblBusinessNatureDAL GetInstance()
        {
            return new mtblBusinessNatureDAL();
        }
        public int InsertENT(mtblBusinessNature ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblBusinessNatures.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblBusinessNature> GetListByENT(mtblBusinessNature ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblBusinessNatures.Where(a => a.BusNatureUID > 0);
            if (ent.BusNatureUID > 0)
                result = result.Where(a => a.BusNatureUID == ent.BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblBusinessNature> GetListByENT(mtblBusinessNature ent)
        {
            var result = model.mtblBusinessNatures.Where(a => a.BusNatureUID > 0);
            if (ent.BusNatureUID > 0)
                result = result.Where(a => a.BusNatureUID == ent.BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(mtblBusinessNature ent)
        {
            var result = model.mtblBusinessNatures.Where(a => a.BusNatureUID > 0);
            if (ent.BusNatureUID > 0)
                result = result.Where(a => a.BusNatureUID == ent.BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblBusinessNature ent)
        {
            nature = model.mtblBusinessNatures.Where(a => a.BusNatureUID == ent.BusNatureUID).FirstOrDefault();
            if (nature != null)
            {
                nature.BusNature_Chi = ent.BusNature_Chi;
                nature.BusNature_Eng = ent.BusNature_Eng;
                nature.Deleted = ent.Deleted;
                nature.LastModifyBy = ent.LastModifyBy;
                nature.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblBusinessNature ent)
        {
            nature = model.mtblBusinessNatures.Where(a => a.BusNatureUID == ent.BusNatureUID).FirstOrDefault();
            if (nature != null)
            {
                model.mtblBusinessNatures.DeleteObject(nature);
            }
            return model.SaveChanges();
        }
    }
}
